package com.wngbms.designpattern;

import org.junit.Test;

import javax.transaction.InvalidTransactionException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TransactionTest {

    @Test
    public void testExecute1() throws InvalidTransactionException {
        Long buyerId = 123L;
        Long sellerId = 234L;
        Long productId = 345L;
        String orderId = "oid";
        Transaction transaction = new Transaction(null, buyerId,
                sellerId, productId, orderId, 23.7D);
        boolean executedResult = transaction.execute();
        assertTrue(executedResult);
    }

    @Test
    public void testExecute2() throws InvalidTransactionException {
        Long buyerId = 123L;
        Long sellerId = 234L;
        Long productId = 345L;
        String orderId = "oid";

        TransactionLock transactionLock = new TransactionLock() {
            @Override
            public boolean lock(String id) {
                return true;
            }

            @Override
            public void unlock(String id) {
            }
        };
        Transaction.MockWalletRpcServiceOne rpcServiceOne = new Transaction.MockWalletRpcServiceOne();

        Transaction transaction = new Transaction(null, buyerId,
                sellerId, productId, orderId, 23.7D);
        transaction.setTransactionLock(transactionLock);
        transaction.setWalletRpcService(rpcServiceOne);

        boolean executedResult = transaction.execute();
        assertTrue(executedResult);
        assertEquals(Transaction.STATUS.EXECUTED, transaction.getStatus());
    }

    @Test
    public void testExecute3() throws InvalidTransactionException {
        Long buyerId = 123L;
        Long sellerId = 234L;
        Long productId = 345L;
        String orderId = "oid";

        TransactionLock transactionLock = new TransactionLock() {
            @Override
            public boolean lock(String id) {
                return true;
            }

            @Override
            public void unlock(String id) {
            }
        };
        Transaction.MockWalletRpcServiceOne rpcServiceOne = new Transaction.MockWalletRpcServiceOne();

        Transaction transaction = new Transaction(null, buyerId,
                sellerId, productId, orderId, 23.7D) {
            @Override
            protected boolean isExpired() {
                return true;
            }
        };
        transaction.setTransactionLock(transactionLock);
        transaction.setWalletRpcService(rpcServiceOne);

        boolean executedResult = transaction.execute();
        assertTrue(executedResult);
        assertEquals(Transaction.STATUS.EXECUTED, transaction.getStatus());
    }

}
